package com.unicair.hancher.disableapp.mvp;

import android.content.pm.PackageManager;

import com.unicair.hancher.disableapp.list.MainAdapter;
import com.unicair.hancher.disableapp.utils.AppInfoBean;
import com.unicair.hancher.disableapp.utils.AppInfoHelper;
import com.unicair.hancher.disableapp.utils.HancherLogUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * 作者：Hancher
 * 时间：2019/1/12.
 * 邮箱：ytu_shaoweijie@163.com
 * 版本：v1.0
 * <p>
 * 说明：
 */
public class MainModel {

    private final PackageManager mPackageManager;

    public MainModel(PackageManager packageManager) {
        mPackageManager = packageManager;
    }

    @Subscribe(threadMode = ThreadMode.ASYNC)
    public void dispatchMainBusEvent(MainMessage message){
        int type = message.getMessageType();
        Object[] objects = message.getObjects();
        switch (type){
            case MainMessage.UPDATE_ALL_DATA:
                updateAllData((Integer)objects[1]);
                break;
        }
    }

    private void updateAllData(int type) {
        ArrayList<AppInfoBean> allAppInfo;
        switch (type) {
            case MainPresenter.MENU_APP_DISABLE:
                allAppInfo = AppInfoHelper.getDisableAppInfo(mPackageManager);
                break;
            case MainPresenter.MENU_APP_ALL:
                allAppInfo = AppInfoHelper.getPackageAppInfo(mPackageManager);
                break;
            case MainPresenter.MENU_APP_LAUNCHER:
            default:
                allAppInfo = AppInfoHelper.getLauncherAppInfo(mPackageManager);
                break;
        }
        HancherLogUtil.d("async query data list="+allAppInfo.size());

        MainMessage message = new MainMessage(MainMessage.UPDATE_ALL_DATA_BACK);
        message.setObject(allAppInfo);
        EventBus.getDefault().post(message);
    }

    public void binder() {
        EventBus.getDefault().register(this);
    }

    public void unBinder() {
        EventBus.getDefault().unregister(this);
    }
}
